package com.mo.ld.activity.view

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dylanc.longan.activity
import com.mo.ld.R
import com.mo.ld.databinding.ActCityselectBinding
import com.mo.lib.constans.CityBean
import com.mo.lib.constans.citySortList
import com.mo.lib.ui.activity.BaseVbActivity
import com.mo.lib.utils.*
import com.mo.lib.view.recyclerview.setItemToTop
import com.mo.lib.view.sideletter.SideLetterBar

/**
 * @ author：mo
 * @ data：2022/9/14:10:00
 * @ 功能：
 */
class CitySelectActivity() : BaseVbActivity<ActCityselectBinding>() {
    override fun initData() {
        viewBinding.sideLetterBar.setOverlay(viewBinding.tvLetterOverlay)

        viewBinding.sideLetterBar.setOnLetterChangedListener(object : SideLetterBar.OnLetterChangedListener {
            override fun onLetterChanged(letter: String?) {
                when (letter) {
                    "定位" -> {
                        viewBinding.krvAllCity.setItemToTop(0)
                    }
//                    "热门" -> {
//                        viewBinding.krvAllCity.setItemToTop(1)
//                    }
                    "A" -> {
                        viewBinding.krvAllCity.setItemToTop(1)
                    }
                    else -> {
                        val first = citySortList.indexOfFirst { it.topc == letter }
                        if (first != -1) {
                            viewBinding.krvAllCity.setItemToTop(first)
                                                }
                    }
                }
            }
        })
        val also = citySortList.also {
            it.add(0, CityBean("定位", "", 1))
        }
        viewBinding.sideLetterBar.setCityList(also)
        viewBinding.krvAllCity.adapter = object : BaseMultiItemQuickAdapter<CityBean, BaseViewHolder>( also) {
            init {
                addItemType(0,R.layout.item_city_selrct)
                addItemType(1,R.layout.city_select_locte)
            }
            override fun convert(holder: BaseViewHolder, item: CityBean) {
                when(holder.itemViewType){
                    0->{
                        val tv_item_city_listview_letter = holder.getView<TextView>(R.id.tv_item_city_listview_letter)
                        val tv_item_city_listview_name = holder.getView<TextView>(R.id.tv_item_city_listview_name)
                        tv_item_city_listview_letter.visibility = if (holder.layoutPosition!=0&&item.topc == data[holder.layoutPosition - 1].topc) View.GONE else View.VISIBLE
                        tv_item_city_listview_letter.text = item.topc
                        tv_item_city_listview_name.text = item.name
                        tv_item_city_listview_name.setClick {
                            showToast("${item.name}:  ${item.center}")
                        }
                    }
                    1->{
                        val tv_located_city = holder.getView<TextView>(R.id.tv_located_city)
                        val iv_located_city = holder.getView<ImageView>(R.id.iv_located_city)
                        val tv_chognxindingwei = holder.getView<TextView>(R.id.tv_chognxindingwei)
                        tv_located_city.setClick {
                            getLoction(tv_located_city)
                        }
                        tv_chognxindingwei.setClick {
                            getLoction(tv_located_city)
                        }
                        getLoction(tv_located_city)
                    }
                }

            }
        }
//            .also {
//            val view = getView(R.layout.item_city_selrct)
//            view.findViewById<TextView>(R.id.tv_item_city_listview_name).text="热门"
//            it.addFooterView(view)
//        }
    }

    private fun getLoction(tvLocatedCity: TextView) {
        actionLoctionOnce {
            it?.let {
                tvLocatedCity.text=it.city(activity)
            }
        }

    }
}